<?php


namespace app\logic\traits;


use app\api\model\Wxapp;
use think\facade\Cache;

use app\logic\ext\HttpRequest;
trait Wx
{
    public function getAccessToken(){
        $HttpRequest=new HttpRequest();
        $access_token=Cache::store("file")->get("wx_access_token");
        if(empty($access_token)){
            $config = Wxapp::find(1)->toArray();
            $url='https://api.weixin.qq.com/cgi-bin/token?';
            $url.="appid={$config['appid']}&secret={$config['appsecret']}&grant_type=client_credential";
            $result = $HttpRequest->curl_get($url);
            $res=json_decode($result,true);
            if(isset($res['access_token'])){
                Cache::store("file")->set("wx_access_token",$res['access_token'],$res['expires_in']);
                return ["code"=>0,"msg"=>$res['access_token']];
            }else{
                return ["code"=>$res['errcode'],"msg"=>$res['errmsg']];
            }
        }
        return ["code"=>0,"msg"=>$access_token];
    }
}